package com.rjkj.medicalwaste.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.paging.AsyncPagingDataDiffer
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.rjkj.medicalwaste.R
import com.rjkj.medicalwaste.bean.*
import com.rjkj.medicalwaste.database.GarbageRequestUnReview

class StorageMainAdapter(private val itemClick: ItemClick<GarbageRequestUnReview>, context: Context):PagingDataAdapter<GarbageRequestUnReview,StorageMainAdapter.ViewHolder>(COMPARATOR) {
    private val textColor1 = ContextCompat.getColor(context, R.color.black)
    private val textColor2 = ContextCompat.getColor(context, R.color.grey_text)

    companion object {
        private val COMPARATOR = object : DiffUtil.ItemCallback<GarbageRequestUnReview>() {
            override fun areItemsTheSame(oldItem: GarbageRequestUnReview, newItem: GarbageRequestUnReview): Boolean {
                return oldItem.garbageQRCode == newItem.garbageQRCode
            }

            override fun areContentsTheSame(oldItem: GarbageRequestUnReview, newItem: GarbageRequestUnReview): Boolean {
                return oldItem == newItem
            }
        }
    }
    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val weight: TextView = itemView.findViewById(R.id.tv_weight)
        val tvDepartment: TextView = itemView.findViewById(R.id.tv_department)
        val tvTimeYmd: TextView = itemView.findViewById(R.id.tv_time_ymd)
        val tvTimeHms: TextView = itemView.findViewById(R.id.tv_time_hms)
        val tvType: TextView = itemView.findViewById(R.id.tv_type)
        val imCheck: ImageView = itemView.findViewById(R.id.im_check)

    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val garbageBean = getItem(position)
        if (garbageBean != null) {
            holder.weight.text = garbageBean.garbageWeight
            holder.tvDepartment.text = garbageBean.departmentName
            garbageBean.garbageTime.split(" ").let {
                if (it.size >= 2) {
                    holder.tvTimeYmd.text = it[0].substring(2, it[0].length)
                    holder.tvTimeHms.text = it[1]
                }
            }
            MedicalWasteType.getTypeByTypeName(garbageBean.garbageType).let {
                holder.tvType.text = it.alias
                if (it.bgId != 0)
                    holder.tvType.setBackgroundResource(it.bgId)
            }

            holder.itemView.setOnClickListener {
                itemClick.onClicked(it,garbageBean)
            }
            if(garbageBean.garbageUserStatus == GarbageStatus.loading){
                holder.tvDepartment.setTextColor(textColor1)
                holder.tvTimeYmd.setTextColor(textColor1)
                holder.tvTimeHms.setTextColor(textColor1)
                holder.weight.setTextColor(textColor1)
                holder.imCheck.visibility = View.GONE
            }else if (garbageBean.garbageUserStatus == GarbageStatus.review){
                holder.tvDepartment.setTextColor(textColor2)
                holder.tvTimeYmd.setTextColor(textColor2)
                holder.tvTimeHms.setTextColor(textColor2)
                holder.weight.setTextColor(textColor2)
                when (garbageBean.reviewWeightState) {
                    ReviewWeightState.CheckSuccess -> {
                        holder.imCheck.visibility = View.VISIBLE
                        holder.imCheck.setImageResource(R.drawable.icon_check_success)
                    }
                    ReviewWeightState.CheckFail -> {
                        holder.imCheck.visibility = View.VISIBLE
                        holder.imCheck.setImageResource(R.drawable.icon_check_fail)
                    }
                    else -> {
                        holder.imCheck.visibility = View.GONE
                    }
                }
            }

        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_storage_main, parent, false)
        return ViewHolder(view)
    }
}